package com.winit.openapi.wh.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundOrderExceptionService;
import com.winit.oms.spi.inbound.command.SearchInboundOrderExceptionCommand;
import com.winit.oms.spi.inbound.vo.InboundOrderExceptionVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;

/**
 * 海外仓入库单异常
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * Winit 	1.0  		2015年12月10日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/inboundOrderException")
public class InboundOrderExceptionController extends BaseController {

    @Resource
    private InboundOrderExceptionService inboundOrderExceptionService;

    /**
     * 查询订单列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();

        SearchInboundOrderExceptionCommand command = null;

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = buildSearchOrderCommand(json);
            Searchable searchable = buildSearchablePageParams(json);
            searchable.addSort(new Sort(Direction.DESC, "CREATED"));
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        // 调用接口分页查询订单
        Page<InboundOrderExceptionVo> pagelist = inboundOrderExceptionService.findInboundOrderException(command);
        if (pagelist != null) {
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", (command.getSearchable().getPage().getPageNumber()));
            pageMap.put("pageSize", command.getSearchable().getPage().getPageSize());
            pageMap.put("totalCount", pagelist.getTotalElements());
            
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", pagelist.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    /**
     * 异常数量
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/count")
    public String count(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        SearchInboundOrderExceptionCommand command = new SearchInboundOrderExceptionCommand();
        command.setCtx(CommandContext.getContext());
        Long count = inboundOrderExceptionService.findExceptionCount(command);
        if (count != null) {
            responseMsg.setData(count);
        }
        return SUCCESS;
    }
    

    private SearchInboundOrderExceptionCommand buildSearchOrderCommand(JSONObject requestData) {
        SearchInboundOrderExceptionCommand command = new SearchInboundOrderExceptionCommand();
        if (requestData.containsKey("orderNo")) {
            command.setOrderNo(requestData.getString("orderNo"));
        } else if (requestData.containsKey("merchandiseCode")) {
            command.setMerchandiseCode(requestData.getString("merchandiseCode"));
        } else if (requestData.containsKey("merchandiseName")) {
            command.setMerchandiseName(requestData.getString("merchandiseName"));
        }
        if (requestData.containsKey("customerCode")) {
            command.setCustomerCode(requestData.getString("customerCode"));
        }
        if (requestData.containsKey("customerOrderNo")) {
            command.setCustomerOrderNo(requestData.getString("customerOrderNo"));
        }
        if (requestData.containsKey("winitProductCode")) {
            command.setWinitProductCode(requestData.getString("winitProductCode"));
        }
        if (requestData.containsKey("entryWhType")) {
            command.setEntryWhType(requestData.getString("entryWhType"));
        }
        if (requestData.containsKey("inspectionWhCode")) {
            command.setInspectionWarehouseCode(requestData.getString("inspectionWhCode"));
        }
        if (requestData.containsKey("destWhCode")) {
            command.setDestinationWarehouseCode(requestData.getString("destWhCode"));
        }
        if (requestData.containsKey("status")) {
            command.setStatus(requestData.getString("status"));
        }
        if (requestData.containsKey("orderDateStart")) {
            String orderDateStart = requestData.getString("orderDateStart");
            orderDateStart += " 00:00:00";
            Date from = DateUtil.parse2Date(orderDateStart, DateUtil.FULL_DATE_STRING);
            command.setDateFrom(from);
        }
        if (requestData.containsKey("orderDateEnd")) {
            String orderDateEnd = requestData.getString("orderDateEnd");
            orderDateEnd += " 23:59:59";
            Date to = DateUtil.parse2Date(orderDateEnd, DateUtil.FULL_DATE_STRING);
            command.setDateTo(to);
        }

        return command;
    }
}
